/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.click.app.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 *
 * @author ariel
 */
@Entity
@Table(name = "employees")
public class Employee extends User implements Serializable {
    
    private static final long serialVersionUID = 1L;

    private String task;
    private Double salarieFixed;
    private Date entryDate;
    private List<Salary> salaries;
    private Boolean finishSalaries;
//    private List<PaymentsCut> paymentsCuts;
//    private List<PaymentsPant> paymentsPants;
    private List<AsistanceControl> asistanceControls;
//    private List<BalanceEmployee> balance;
    private List<PeriodPaymentEmployee> periodPaymentEmployees;

    @Column(length = 100)
    public String getTask() {
        return task;
    }

    public void setTask(String task) {
        this.task = task;
    }

    @Column(name = "salarie_fixed")
    public Double getSalarieFixed() {
        return salarieFixed;
    }

    public void setSalarieFixed(Double salarieFixed) {
        this.salarieFixed = salarieFixed;
    }

    @Column(name = "entry_date")
    @Temporal(TemporalType.DATE)
    public Date getEntryDate() {
        return entryDate;
    }

    public void setEntryDate(Date entryDate) {
        this.entryDate = entryDate;
    }

    @OneToMany(mappedBy = "employee", fetch=FetchType.EAGER)
    public List<Salary> getSalaries() {
        if (salaries==null){
            salaries = new ArrayList<Salary>();
        }
        return salaries;
    }

    public void setSalaries(List<Salary> salaries) {
        this.salaries = salaries;
    }

    @Column(name = "finish_salaries")
    public Boolean getFinishSalaries() {
        return finishSalaries;
    }

    public void setFinishSalaries(Boolean finishSalaries) {
        this.finishSalaries = finishSalaries;
    }

    @OneToMany(mappedBy="employee")
    public List<PeriodPaymentEmployee> getPeriodPaymentEmployees() {
        return periodPaymentEmployees;
    }

    public void setPeriodPaymentEmployees(List<PeriodPaymentEmployee> periodPaymentEmployees) {
        this.periodPaymentEmployees = periodPaymentEmployees;
    }

//    @OneToMany(mappedBy="employee")
//    public List<PaymentsCut> getPaymentsCuts() {
//        return paymentsCuts;
//    }
//
//    public void setPaymentsCuts(List<PaymentsCut> paymentsCuts) {
//        this.paymentsCuts = paymentsCuts;
//    }

//    @OneToMany(mappedBy="employee")
//    public List<PaymentsPant> getPaymentsPants() {
//        return paymentsPants;
//    }

//    public void setPaymentsPants(List<PaymentsPant> paymentsPants) {
//        this.paymentsPants = paymentsPants;
//    }

//    @OneToMany(mappedBy = "employee")
//    public List<BalanceEmployee> getBalance() {
//        return balance;
//    }
//
//    public void setBalance(List<BalanceEmployee> balance) {
//        this.balance = balance;
//    }



    @Override
    public int hashCode() {
        int hash = 0;
        hash += (getId() != null ? getId().hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Employee)) {
            return false;
        }
        Employee other = (Employee) object;
        if ((this.getId() == null && other.getId() != null) || (this.getId() != null && !this.getId().equals(other.getId()))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return  super.toString() + (new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).append(this.getTask())).toString();
    }

    /**
     * @return the asistanceControls
     */
    @OneToMany(mappedBy="employee", cascade=CascadeType.REMOVE)
    public List<AsistanceControl> getAsistanceControls() {
        if (asistanceControls==null){
            asistanceControls= new ArrayList<AsistanceControl>();
        }
        return asistanceControls;
    }

    /**
     * @param asistanceControls the asistanceControls to set
     */
    public void setAsistanceControls(List<AsistanceControl> asistanceControls) {
        this.asistanceControls = asistanceControls;
    }

    @Transient
    public Role getRoleByName(String nameRole){
        Role roleRes = null;
        for (Role role : this.getRoles()) {
            if(role.getName().equals(nameRole)){
                roleRes = role;
                break;
            }
        }
        return roleRes;
    }

    @Transient
    public boolean isEmployeeCutter(){
        boolean res = false;
        for (Iterator<Role> it = roles.iterator(); it.hasNext();) {
            Role role = it.next();
            if(role.getName().equals("ROLE_EMPLOYEE_CUTTER")){
                res = true;
                break;
            }
        }
        return res;
    }

    @Transient
    public boolean isEmployeeDressmaker(){
        boolean res = false;
        for (Iterator<Role> it = roles.iterator(); it.hasNext();) {
            Role role = it.next();
            if(role.getName().equals("ROLE_EMPLOYEE_DRESSMAKER")){
                res = true;
                break;
            }
        }
        return res;
    }

    @Transient
    public boolean isEmployeeFixed(){
        boolean res = false;
        for (Iterator<Role> it = roles.iterator(); it.hasNext();) {
            Role role = it.next();
            if(role.getName().equals("ROLE_EMPLOYEE_FIXED")){
                res = true;
                break;
            }
        }
        return res;
    }

    public void clearEmployeeRoles(User user){
        getRoles().clear();
        for(Role r : user.getRoles()){
            if(!r.getName().equals("ROLE_EMPLOYEE_CUTTER") && !r.getName().equals("ROLE_EMPLOYEE_DRESSMAKER") && !r.getName().equals("ROLE_EMPLOYEE_FIXED"))
                addRole(r);
        }
    }
}
